﻿package com.toncen.samepms.basic.dao;

import java.util.List;

import com.toncen.samepms.basic.domain.Organization;
import com.toncen.samepms.common.AbstractDao;
import com.toncen.samepms.common.IDomain;

/**
 * 描述：表BASIC_ORGANIZATION的dao文件
 * 
 * @author tch
 */
public class OrganizationDao<O> extends AbstractDao<Organization> {

	public OrganizationDao() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractDao#add(com.toncen.samepms.common.AbstractDomain)
	 */
	@Override
	public long add(Organization domain) throws Exception {
		long result = super.add(domain);
		new OrganizationTypeDao().save(domain.getTypes());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractDao#delete(long)
	 */
	@Override
	public int delete(long id) throws Exception {
		new OrganizationTypeDao().delByOrganizationId(id);
		return super.delete(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractDao#update(com.toncen.samepms.common.AbstractDomain)
	 */
	@Override
	public int update(Organization domain) throws Exception {
		new OrganizationTypeDao().delByOrganizationId(domain.getId());
		new OrganizationTypeDao().save(domain.getTypes());
		return super.update(domain);
	}

	/**
	 * 获取全部组织(排除指定的组织)
	 * 
	 * @param exclude
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Organization> getAllOrganizationExclude(Organization exclude) throws Exception {
		return findList("getAllOrganizationExclude", exclude == null ? IDomain.UNSAVED_ID : exclude.getId());
	}

	/**
	 * @param state
	 * @return
	 * @throws Exception
	 */
	public List<Organization> getAllByState(int state) throws Exception {
		return findList("getAllByState", state);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.db.BaseDao#initialize()
	 */
	@Override
	protected void initialize() {
		setStatementPrefix("Organization");
		setStatementDivision(".");
	}

	/**
	 * 获取一级根组织
	 * 
	 * @param exclude
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Organization> getAllRootOrganization() throws Exception {
		return findList("getAllRootOrganization", null);
	}
}
